package com.itheima.goods.service.impl;

import com.itheima.goods.entity.GoodsEntity;
import com.itheima.goods.mapper.GoodsMapper;
import com.itheima.goods.service.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.concurrent.TimeUnit;

@Service
public class GoodsServiceImpl implements GoodsService {

    @Autowired
    private GoodsMapper goodsMapper;

    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    public GoodsEntity findGoodsInfoById(Long goodsId) {

      /*  //布隆过滤器校验
        BloomFilterUtil bloomFilterUtil = new BloomFilterUtil();
        //判断key是否存在
        if (!bloomFilterUtil.contains("goodsId:" + goodsId)){
            //布隆过滤器中没有
            return null;
        }
*/
        //先查询缓存
        GoodsEntity redisGoodsEntity = (GoodsEntity) redisTemplate.opsForValue().get("goodsId:" + goodsId);

        if (redisGoodsEntity != null) {
            return redisGoodsEntity;
        }

        //缓存中没有,查询数据库
        GoodsEntity goodsEntity = goodsMapper.selectById(goodsId);
        //数据添加到缓存
        redisTemplate.opsForValue().set("goodsId:" + goodsId, goodsEntity, 30, TimeUnit.MINUTES);
        //将添加进缓存的key 添加到布隆过滤器中
        //    bloomFilterUtil.add("goodsId:" + goodsId);

        return goodsEntity;
    }

    //修改
    @Override
    public GoodsEntity updateInfo(Long goodsId, GoodsEntity goodsEntity) {
        return null;
    }
}
